package com.ffapp.demo.bean;


import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.ffapp.demo.basis.Constants;

import java.util.ArrayList;
import java.util.Iterator;

import my.FileUtils;
import my.LogUtil;

public class VideoCatalogListBean extends BaseListBean {

    public final static String PARENT_ID_TOP  = "top";

    private ArrayList<VideoCatalogListData> list = new ArrayList<VideoCatalogListData>();

    private ArrayList<VideoCatalogListData> listResult = new ArrayList<>();

    public ArrayList<VideoCatalogListData> getList() {
        return list;
    }

    public void setList(ArrayList<VideoCatalogListData> list) {
        this.list = list;
    }

    @JSONField(serialize = false)
    public void processList(){
       listResult= new ArrayList<>();
       int level = 0;
        for(VideoCatalogListData userZoneListData:list){
//            userZoneListData.setLevel(level);
            userZoneListData.setParentId(PARENT_ID_TOP);
            addChileDataInList(userZoneListData,listResult,level);
        }
        LogUtil.i("getItemByListPosition---size=--->"+listResult.size());
    }
    @JSONField(serialize = false)
    private void addChileDataInList(VideoCatalogListData data,ArrayList<VideoCatalogListData> list,int currentLevel){
        data.setLevel(currentLevel);
        list.add(data);
        if( !data.isVideo_BOOLEAN() && data.getChildren().size() > 0 && data.isExpand()){
            for(VideoCatalogListData userZoneListData:data.getChildren()){
                userZoneListData.setParentId(data.getId());
                addChileDataInList(userZoneListData,list,currentLevel+1);
            }
        }
    }

    @JSONField(serialize = false)
    public int getListResultCount(){
        return listResult == null?0:listResult.size();
    }

    @JSONField(serialize = false)
    public VideoCatalogListData getItemByListPosition(int position){
        return listResult.get(position);
    }

    @JSONField(serialize = false)
    public void expandOrCloseByListPosition(int position){
        VideoCatalogListData  listData =  getItemByListPosition(position);
//        LogUtil.i("expandOrCloseByListPosition--->"+position);
//        LogUtil.i("expandOrCloseByListPosition--->"+listData.getTitle());
//        LogUtil.i("expandOrCloseByListPosition--->"+listData.getChildren().size());
//        LogUtil.i("expandOrCloseByListPosition--level->"+ listData.getLevel());

        for(VideoCatalogListData data:list){
            changeExpandById(data,listData.getId());
        }
        processList();
    }
    @JSONField(serialize = false)
    private boolean changeExpandById(VideoCatalogListData data,String id){
        if(data.getId().equals(id)){
            data.setExpand(!data.isExpand());
            return true;
        }
        if(!data.isVideo_BOOLEAN() && data.getChildren().size() > 0 ){
            for(VideoCatalogListData userZoneListData:data.getChildren()){
                if(changeExpandById(userZoneListData,id))return true;
            }
        }
        return false;
    }



    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "ff_firebrige_contact_list.data";

    @JSONField(serialize = false)
    public static VideoCatalogListBean getFromCache() {
        VideoCatalogListBean data = (VideoCatalogListBean) FileUtils.readObject(FILE_CACHE);
        if (data == null) {
//            data = new ContactListBean();
        }
        return data;
    }

    @JSONField(serialize = false)
    public static boolean isCacheExist(){
        return FileUtils.isFileExist(FILE_CACHE);
    }

    @JSONField(serialize = false)
    public static long getLastCacheTime(){
        return FileUtils.getLasModified(FILE_CACHE);
    }


    @JSONField(serialize = false)
    public void save() {
        FileUtils.cacheObject(FILE_CACHE, this);
    }
    @JSONField(serialize = false)
    public static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
    }

    public void addOrRemoveData(VideoCatalogListData data){
        Iterator<VideoCatalogListData> iterator = list.iterator();
        while (iterator.hasNext()){
            VideoCatalogListData  itemData = iterator.next();
            if(data.getId().equals(itemData.getId())){
                iterator.remove();
                return;
            }
        }
        list.add(data);
    }

    public boolean isDateChecked(VideoCatalogListData data){
        for (VideoCatalogListData itemData:list){
            if(data.getId().equals(itemData.getId())){
                return true;
            }
        }
        return false;
    }

    @JSONField(serialize = false)
    public VideoCatalogListData getItemById(String id){
        if(TextUtils.isEmpty(id))return null;
        if(id.equals(PARENT_ID_TOP)){
            VideoCatalogListData mListBeanCatalogNow = new VideoCatalogListData();
            mListBeanCatalogNow.setChildren(getList());
            return mListBeanCatalogNow;
        }
        for (VideoCatalogListData itemData:listResult){
            if(id.equals(itemData.getId())){
                return itemData;
            }
        }

        return null;
    }
//    @JSONField(serialize = false)
//    private void addChileDataInList(VideoCatalogListData data){
//        if( !data.isVideo_BOOLEAN() && data.getChildren().size() > 0 && data.isExpand()){
//            for(VideoCatalogListData userZoneListData:data.getChildren()){
//                userZoneListData.setParentId(data.getId());
//                addChileDataInList(userZoneListData,list,currentLevel+1);
//            }
//        }
//    }
}
